<template>
    <div class="page">


    </div>
</template>

<script>
    export default {
        name: 'index',

        data() {
            return {

            }
        },

		beforeRouteEnter(to, from, next) {
			if(to.name=='INDEX' && from.name=='LOGIN'){
				if(parent.WeixinJSBridge){
					parent.WeixinJSBridge.call('closeWindow');
				}

			}else{
				next();
			}
		},

        mounted() {
        	//验证版本号
			const version = '1.0.0';
			if(version != this.$Cookies.get('version')){
				this.$Cookies.set('version', version);
				this.$Cookies.remove('personName');
				this.$Cookies.remove('personId');
				this.$Cookies.remove('personPhone');
				this.$Cookies.remove('personRole');
				this.$Cookies.remove('communityName');
				this.$Cookies.remove('communityId');
				this.$Cookies.remove('userType');
            }


			const code = this.getUrlParam('code');  // 获取路由中的code
			if (code == null || code === '') {
				const local = window.location.href.split('#')[0];
				//const local = 'http://jinlanghorse.cn?userType=1000';
				window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx15e3c7eb2f5c83e1&redirect_uri='
					+ encodeURIComponent(local) + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';

			} else {
				this.getOpenId(code);
				this.$Cookies.set('userType', this.getUrlParam('userType'));
			}


//            this.$Cookies.set('userType', '3000');
//			this.$Cookies.set('personRole', 'propertyManager');  //securityStaff保安; propertyManager经理;
        },

        methods: {
            //获取url中的参数
			getUrlParam(name) {
				let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
				let url = window.location.href.split('#')[0];
				let search = url.split('?')[1];
				if (search) {
					let r = search.substr(0).match(reg);
					if (r !== null){
						return unescape(r[2]);
					}

					return null

				} else {
					return null
				}
			},

			//获取openId
			getOpenId(code){
				this.$axios.post(this.$api.getWxUserInfo, {
					code: code,
					scope: 'snsapi_userinfo',

				}).then(success => {
					if(success.data.code == '200'){
						this.$Cookies.set('openId', success.data.data.openid);
						this.$Cookies.set('headImgUrl', success.data.data.headimgurl);
						this.$router.push('/login');
					}

				}).catch(error => {

				})
			},


        },
    }
</script>

<style lang="scss" scoped>

</style>

